package cn.com.dhcc.turbo.service.registry.sync.mina;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.dhcc.app.core.handler.remotehandler.socket.ISocketHandler;
import cn.com.dhcc.app.core.handler.remotehandler.socket.SocketSession;
import cn.com.dhcc.app.core.service.Log;
import cn.com.dhcc.app.core.service.Log.LOGTYPE;
import cn.com.dhcc.app.core.tag.CommonStaticCode.SUCCESSFAIL;
import cn.com.dhcc.app.core.util.JsonUtil;
import cn.com.dhcc.app.pub.core.consts.AppIocBean;
import cn.com.dhcc.turbo.entity.registry.DomainInfo;
import cn.com.dhcc.turbo.entity.registry.NodeInfo;
import cn.com.dhcc.turbo.entity.registry.NodePipeline;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo;
import cn.com.dhcc.turbo.entity.registry.SynchronizeInfo.SyncType;
import cn.com.dhcc.turbo.service.registry.sync.SyncCallBack;
import cn.com.dhcc.turbo.service.registry.sync.SynchronizeHandler;
import cn.com.dhcc.turbo.service.registry.vo.NodeRegistSyncData;

/**
 * socket接收数据
 * @author qiao
 * @2014-8-26 @下午6:47:02
 */
@Service(AppIocBean.TURBO_SOCKETIO_HANDLER)
public class TurboSocketIoHandler implements ISocketHandler {

	private Logger log = Log.getLogger(LOGTYPE.SOCKET);
	@Autowired
	SynchronizeHandler synchronizeHandler;
	
	@SuppressWarnings("unchecked")
	@Override
	public void onMessageReceived(SocketSession socketSession,
			Object receivedData) {
		if(receivedData instanceof SynchronizeInfo){
			SynchronizeInfo synchronizeInfo = (SynchronizeInfo)receivedData;
			String dataType = synchronizeInfo.getDataType();
			String dataContent = synchronizeInfo.getDataContent();
			NodeInfo fromNode = synchronizeInfo.getFromNodeInfo();
			NodeInfo srcNode = synchronizeInfo.getSrcNodeInfo();
			if(SyncType.NODE_REGIST.getCode().equalsIgnoreCase(dataType)){//处理节点注册时的批量同步
				NodeRegistSyncData nodeRegistSyncData = JsonUtil.toObject(dataContent, NodeRegistSyncData.class);
				try {
					SyncCallBack syncCallBack = synchronizeHandler.receiveNodeRegistSyncData(fromNode,srcNode,nodeRegistSyncData);
					socketSession.sendData(syncCallBack);
				} catch (Exception e) {
					log.error(e);
				}
			}else if(SyncType.NODE_INFO.getCode().equalsIgnoreCase(dataType)){//处理节点的增删改查同步
				NodeInfo srcNodeInfo = JsonUtil.toObject(dataContent, NodeInfo.class);
				SyncCallBack syncCallBack = synchronizeHandler.receiveNodeInfoSyncData(fromNode,srcNodeInfo);
				socketSession.sendData(syncCallBack);
			}else if(SyncType.DOMAIN_INFO.getCode().equalsIgnoreCase(dataType)){//处理域的增删改查同步
				DomainInfo srcDomainInfo = JsonUtil.toObject(dataContent, DomainInfo.class);
				SyncCallBack syncCallBack = synchronizeHandler.receiveDomainInfoSyncData(fromNode,srcNode,srcDomainInfo);
				socketSession.sendData(syncCallBack);
			}/*else if(SyncType.SYS_INFO.getCode().equalsIgnoreCase(dataType)){//处理注册系统的增删改查同步
				SysInfo srcSysInfo = JsonUtil.toObject(dataContent, SysInfo.class);
				SyncCallBack syncCallBack = synchronizeHandler.receiveSysInfoSyncData(fromNode,srcNode,srcSysInfo);
				socketSession.sendData(syncCallBack);
			}*/else if(SyncType.PILE_LINE.getCode().equalsIgnoreCase(dataType)){//处理通道同步信息的同步
				NodePipeline srcPipeline = JsonUtil.toObject(dataContent, NodePipeline.class);
				SyncCallBack syncCallBack = synchronizeHandler.receivePipelineSyncData(fromNode,srcNode,srcPipeline);
				socketSession.sendData(syncCallBack);
			}else if(SyncType.PILE_LINE_LIST.getCode().equalsIgnoreCase(dataType)){//处理多个通道同步信息的同步
				List<NodePipeline> srcPipelines = JsonUtil.toObject(dataContent, List.class);
				SyncCallBack syncCallBack = synchronizeHandler.receivePipelineListSyncData(fromNode,srcNode,srcPipelines);
				socketSession.sendData(syncCallBack);
			}else if(SyncType.MONITOR.getCode().equalsIgnoreCase(dataType)){//处理管理范围内监控信息的同步
				/*NodeInfo nodeInfo = (NodeInfo) receivedData;
				synchronizeHandler.receiveNodeInfoSyncData(nodeInfo);*/
			}else if(SyncType.CREATE_REL.getCode().equalsIgnoreCase(dataType)){//处理建立交换关系的同步
				/*NodeInfo nodeInfo = (NodeInfo) receivedData;
				SyncCallBack syncCallBack = synchronizeHandler.receiveNodeInfoSyncData(nodeInfo);
				socketSession.sendData(syncCallBack);*/
			}else if(SyncType.TEST_CONNECT.getCode().equalsIgnoreCase(dataType)){//连接测试
				SyncCallBack syncCallBack = new SyncCallBack();
				syncCallBack.setResult(SUCCESSFAIL.SUCCESS);
				socketSession.sendData(syncCallBack);
			}else{
				log.warn("收到不支持的同步信息，"+receivedData);
			}
		}else{
			log.warn("收到不支持的socket信息，"+receivedData);
			SyncCallBack syncCallBack = new SyncCallBack();
			syncCallBack.setResult(SUCCESSFAIL.FAIL);
			socketSession.sendData(syncCallBack);
		}
	}

	@Override
	public void onExceptionCaught(SocketSession socketSession, Throwable cause) {
		// TODO Auto-generated method stub

	}

}
